//获取路由对象方法
const getRouter = require('router');
const router = getRouter();
//获取集合构造函数
const Student = require('../model/user');
const querystring = require('querystring');
//获取模板引擎
const template = require('art-template');

//呈现add页面
router.get('/add',(req, res)=>{
    let html = template('index', {});
    res.end(html);
});
//呈现list页面
router.get('/list',async (req,res)=>{
    let students = await Student.find();
    let html = template('list', {
        students: students
    });
    res.end(html);
});
//处理添加学院的请求
router.post('/add',(req, res)=>{
    let formData = '';
    req.on('data',(params) => {
        formData += params;
    });
    req.on('end',async ()=>{
        let user = querystring.parse(formData);
        await Student.create(user);
        res.writeHead(301,{
            Location: '/list'
        });
        res.end();
    })
});
module.exports = router;